package com.vipheyue.bigcompany.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;
import com.lidroid.xutils.util.LogUtils;
import com.vipheyue.bigcompany.R;
import com.vipheyue.bigcompany.adapter.CommonAdapter;
import com.vipheyue.bigcompany.adapter.ViewHolder;
import com.vipheyue.bigcompany.domain.Job;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class PublishedActivity extends Activity {

    @Bind(R.id.pull_refresh_list)
    PullToRefreshListView mPullRefreshListView;
    private CommonAdapter<Job> mAdapter;
    private List<Job> mDatas = new ArrayList<Job>();
    private int nowPageNum = 0;
    private int allPageNum = 0;
    private int pageSize = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_published);
        ButterKnife.bind(this);
        if (mDatas != null) {
            mDatas.clear();
        }
        LogUtils.customTagPrefix = "xUtils";  LogUtils.allowI = true;
        initData(0);
        initRefresh();
    }

    private void initRefresh() {
        LogUtils.i("initRefresh");

        // Set a listener to be invoked when the list should be refreshed.
        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(PublishedActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show();

                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                // Do work to refresh the list here.
                mDatas.clear();
                initData(0);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(PublishedActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show();

                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                // Do work to refresh the list here.
                initData(nowPageNum);
            }

        });
        // Add an end-of-list listener
        mPullRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                Toast.makeText(PublishedActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
            }
        });
        ListView actualListView = mPullRefreshListView.getRefreshableView();

        mAdapter = new CommonAdapter<Job>(this, mDatas, R.layout.job_item) {
            @Override
            public void convert(ViewHolder helper, Job item) {

            }
        };
        /**
         * Add Sound Event Listener
         */
        SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
        soundListener.addSoundEvent(PullToRefreshBase.State.PULL_TO_REFRESH, R.raw.pull_event);
        soundListener.addSoundEvent(PullToRefreshBase.State.RESET, R.raw.reset_sound);
        soundListener.addSoundEvent(PullToRefreshBase.State.REFRESHING, R.raw.refreshing_sound);
        mPullRefreshListView.setOnPullEventListener(soundListener);
        // You can also just use setListAdapter(mAdapter) or
         mPullRefreshListView.setAdapter(mAdapter);
//        actualListView.setAdapter(mAdapter);
    }

    private void initData(int getNumPage) {
        LogUtils.i("----------------------> initData ");
        BmobQuery<Job> query = new BmobQuery<Job>();
        query.setLimit(pageSize); // 限制最多10条数据结果作为一页
        query.setSkip(pageSize * getNumPage); // 忽略前10条数据（即第一页数据结果）
        query.include("publisherCompany");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(this, new FindListener<Job>() {
            @Override
            public void onSuccess(List<Job> object) {
                LogUtils.i("onsuccess");
                mDatas.addAll(object);
                nowPageNum = nowPageNum + 1;
                mAdapter.notifyDataSetChanged();
                // Call onRefreshComplete when the list has been refreshed.
                mPullRefreshListView.onRefreshComplete();
            }

            @Override
            public void onError(int code, String msg) {
                // TODO Auto-generated method stub
                mPullRefreshListView.onRefreshComplete();

            }
        });


    }

}
